package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DishMapper {
    @AutoFill(value = OperationType.INSERT)
    @Options(keyProperty = "id",useGeneratedKeys = true)
    @Insert("INSERT INTO sky_take_out.dish ( name, category_id, price, image, description, status, create_time, update_time, create_user, update_user) VALUES " +
            "( #{name}, #{categoryId}, #{price}, #{image}, #{description}, #{status}, #{createTime}, #{updateTime},#{createUser},#{updateUser});")
    void insert(Dish dish);

    Page<DishVO> select(DishPageQueryDTO dto);

    /**
     * 根据分类id查询改分类下菜品数量
     * @param id
     * @return
     */
    @Select("select count(*) from dish where category_id = #{id}")
    int selectByCategoryId(Integer id);

    void delete(List<Long> ids);

    /**
     * 查询删除的菜品中起售状态数量
     * @param ids
     * @return
     */
    Integer queryStatus(List<Long> ids);
    /**
     * 查询准备起售的套餐中菜品的未起售状态数量
     * @param ids
     * @return
     */
    Integer queryStatusStop(List<Long> ids);

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Select("select * from dish where id = #{id}")
    Dish selectById(Long id);

    /**
     * 修改菜品信息
     * @param dish
     */

    @AutoFill(value = OperationType.UPDATE)
    void update(Dish dish);

    /**
     * 根据分类查询菜品
     * @param id
     * @return
     */
    @Select("select * from dish where category_id = #{id} and status = 1")
    List<Dish> queryDishByCategoryId(Long id);


    /**
     * 套餐中包含的菜品未起售的数量
     * @param id
     * @return
     */
    @Select("select count(*) from dish d left join setmeal_dish sd on d.id = sd.dish_id where sd.setmeal_id = #{id} and d.status = 0 ")
    int selectCountBySetmealId(Long id);

    /**
     * 查询菜品被哪些套餐关联了
     * @param id
     * @return
     */

    @Select("select sd.setmeal_id from setmeal_dish sd left join dish d\n" +
            "                                            on d.id = sd.id\n" +
            "where sd.dish_id = #{id};")
    List<Long> selectCountById(Long id);

    List<Dish> list(Dish dish);
    /**
     * 根据条件统计菜品数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    void insertBatch(List<Dish> dishList);

}
